package com.lelouch.service;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.lelouch.bean.SysDept;
import com.lelouch.bean.SysRole;
import com.lelouch.common.RequestHolder;
import com.lelouch.dao.SysDeptMapper;
import com.lelouch.dao.SysRoleMapper;
import com.lelouch.dao.SysRoleUserMapper;
import com.lelouch.exception.ParamException;
import com.lelouch.param.DeptParam;
import com.lelouch.param.RoleParam;
import com.lelouch.util.BeanValidator;
import com.lelouch.util.IpUtil;
import com.lelouch.util.LevelUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 部门服务
 *
 * @author lcy
 * @create 2018-06-12 15:08
 */
@Service
public class SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;
    @Autowired
    private SysLogService sysLogService;

    public void save(RoleParam roleParam) {
        //检查参数是否合法
        BeanValidator.check(roleParam);
        //检查角色名是否重复
        if (checkExist(roleParam.getName(), roleParam.getId())) {
            throw new ParamException("系统中存在相同的角色名称，请重新命名！");
        }
        SysRole sysRole = SysRole.builder().name(roleParam.getName()).remark(roleParam.getRemark()).status(roleParam.getStatus())
                .type(roleParam.getType()).build();

        sysRole.setOperator(RequestHolder.getUser().getUsername());
        sysRole.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getRequest()));
        sysRole.setOperateTime(new Date());
        sysRoleMapper.insertSelective(sysRole);
        sysLogService.saveRoleLog(null,sysRole);
    }


    public void update(RoleParam roleParam) {
        BeanValidator.check(roleParam);
        //检查角色名是否重复
        if (checkExist(roleParam.getName(), roleParam.getId())) {
            throw new ParamException("系统中存在相同的角色名称，请重新命名！");
        }
        SysRole before = sysRoleMapper.selectByPrimaryKey(roleParam.getId());
        Preconditions.checkNotNull(before, "待更新的角色不存在");


        //生成level的名称
        SysRole after = SysRole.builder().id(roleParam.getId()).name(roleParam.getName()).remark(roleParam.getRemark()).status(roleParam.getStatus())
                .type(roleParam.getType()).build();
        after.setOperator(RequestHolder.getUser().getUsername());
        after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getRequest()));
        after.setOperateTime(new Date());
        sysRoleMapper.updateByPrimaryKeySelective(after);
        sysLogService.saveRoleLog(before,after);
    }

    private boolean checkExist(String name, Integer id) {
        return sysRoleMapper.countByNameAndId(name, id) > 0;
    }

    public List<SysRole> getAll() {
        return sysRoleMapper.getAll();
    }

    public List<SysRole> getRoleListByUserId(Integer userId) {
        List<Integer> roleIds = sysRoleUserMapper.getRoleListByUserId(userId);
        if (CollectionUtils.isEmpty(roleIds)) {
                return Lists.newArrayList();
        }
        return sysRoleMapper.getRoleListByIdList(roleIds);
    }


}
